package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cyf
 * @version 1.0
 * @description #TODO
 * @date 2024/9/2 20:26
 **/
@Slf4j
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Override
    public CourseTeacher queryCourseTeacherByCourseId(Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, id);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        return courseTeacher;
    }

    @Override
    public CourseTeacher addCourseTeacher(CourseTeacherDto courseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert<=0){
            XueChengPlusException.cast("添加课程教师失败");
        }
        return courseTeacher;
    }

    @Override
    public CourseTeacher updateCourseTeacher(CourseTeacherDto courseTeacherDto) {
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(courseTeacherDto, courseTeacher);
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseTeacherDto.getCourseId());
        queryWrapper.eq(CourseTeacher::getTeacherName, courseTeacherDto.getTeacherName());
        CourseTeacher ct = courseTeacherMapper.selectOne(queryWrapper);
        if(ct!=null){
            XueChengPlusException.cast("修改成重复的课程教师名和课程，请仔细检查");
            return null;
        }else {
            int update = courseTeacherMapper.updateById(courseTeacher);
            if (update <= 0) {
                XueChengPlusException.cast("修改课程教师失败");
            }
            return courseTeacher;
        }
    }

    @Override
    public void deleteCourseTeacher(Long courseId, Long teachId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        queryWrapper.eq(CourseTeacher::getId,teachId);
        int delete = courseTeacherMapper.delete(queryWrapper);
        if(delete<=0){
            XueChengPlusException.cast("删除课程教师失败");
        }
    }
}
